package com.melesta.engine;

import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class BackupAgent extends android.app.backup.BackupAgent
{
  private static final String BACKUP = "BACKUP";
  private static final String BACKUP_TRANSACTIONS = "BACKUP2";

  public BackupAgent()
  {
    Log.i("engine", "BackupAgent()");
  }

  static byte[] backupFiles(BackupDataOutput paramBackupDataOutput, DatabaseHelper paramDatabaseHelper)
    throws IOException
  {
    byte[] arrayOfByte = DbBackup.backupFiles(paramDatabaseHelper);
    int i = arrayOfByte.length;
    if (paramBackupDataOutput != null)
    {
      paramBackupDataOutput.writeEntityHeader("BACKUP", i);
      paramBackupDataOutput.writeEntityData(arrayOfByte, i);
    }
    return arrayOfByte;
  }

  static byte[] backupTransactions(BackupDataOutput paramBackupDataOutput, DatabaseHelper paramDatabaseHelper)
    throws IOException
  {
    byte[] arrayOfByte = DbBackup.backupTransactions(paramDatabaseHelper);
    int i = arrayOfByte.length;
    if (paramBackupDataOutput != null)
    {
      paramBackupDataOutput.writeEntityHeader("BACKUP2", i);
      paramBackupDataOutput.writeEntityData(arrayOfByte, i);
    }
    return arrayOfByte;
  }

  private void restoreFiles(BackupDataInput paramBackupDataInput, DatabaseHelper paramDatabaseHelper, int paramInt)
    throws IOException
  {
    byte[] arrayOfByte = new byte[paramInt];
    paramBackupDataInput.readEntityData(arrayOfByte, 0, paramInt);
    DbBackup.restoreFiles(paramDatabaseHelper, arrayOfByte);
  }

  private void restoreTransactions(BackupDataInput paramBackupDataInput, DatabaseHelper paramDatabaseHelper, int paramInt)
    throws IOException
  {
    byte[] arrayOfByte = new byte[paramInt];
    paramBackupDataInput.readEntityData(arrayOfByte, 0, paramInt);
    DbBackup.restoreTransactions(paramDatabaseHelper, arrayOfByte);
  }

  public void onBackup(ParcelFileDescriptor paramParcelFileDescriptor1, BackupDataOutput paramBackupDataOutput, ParcelFileDescriptor paramParcelFileDescriptor2)
    throws IOException
  {
    try
    {
      Log.i("engine", "BackupAgent-store");
      DatabaseHelper localDatabaseHelper = DatabaseHelper.getHelper(this);
      backupFiles(paramBackupDataOutput, localDatabaseHelper);
      backupTransactions(paramBackupDataOutput, localDatabaseHelper);
      FileOutputStream localFileOutputStream = new FileOutputStream(paramParcelFileDescriptor2.getFileDescriptor());
      DataOutputStream localDataOutputStream = new DataOutputStream(localFileOutputStream);
      localDataOutputStream.writeLong(System.currentTimeMillis());
      localDataOutputStream.close();
      localFileOutputStream.close();
      return;
    }
    catch (Exception localException)
    {
      Error.processException(localException);
    }
  }

  public void onRestore(BackupDataInput paramBackupDataInput, int paramInt, ParcelFileDescriptor paramParcelFileDescriptor)
    throws IOException
  {
    while (true)
    {
      DatabaseHelper localDatabaseHelper;
      String str;
      int i;
      try
      {
        Log.i("engine", "BackupAgent-restore " + paramParcelFileDescriptor.toString());
        localDatabaseHelper = DatabaseHelper.getHelper(this);
        if (localDatabaseHelper.getFilesList(false).size() > 0)
          return;
        if (!paramBackupDataInput.readNextHeader())
          break;
        str = paramBackupDataInput.getKey();
        i = paramBackupDataInput.getDataSize();
        Log.d("engine", "BackupAgent-restore(" + str + ") " + i);
        if ("BACKUP".equals(str))
        {
          restoreFiles(paramBackupDataInput, localDatabaseHelper, i);
          continue;
        }
      }
      catch (Exception localException)
      {
        Error.processException(localException);
        return;
      }
      if ("BACKUP2".equals(str))
        restoreTransactions(paramBackupDataInput, localDatabaseHelper, i);
    }
    FileOutputStream localFileOutputStream = new FileOutputStream(paramParcelFileDescriptor.getFileDescriptor());
    DataOutputStream localDataOutputStream = new DataOutputStream(localFileOutputStream);
    localDataOutputStream.writeLong(System.currentTimeMillis());
    localDataOutputStream.close();
    localFileOutputStream.close();
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.melesta.engine.BackupAgent
 * JD-Core Version:    0.6.2
 */